// 
//  chs-alert.js
//  主要功能：为某个文档元素绑定点击弹窗，选择列表里面任意一项，返回该项对应的数据，数据为json对象。
//  
//  Created by wangyunlong on 2017-04-10.
//  Copyright 2017 wangyunlong. All rights reserved.
// 

var chs_alert = function(options){
	var self = this;
	self.coditions = {};
	self.coditions.page = 1;
	self.coditions.pageNum = 20;
	self.pageData = [];
	self.handle = options.handle;
	self.eventStr = options.eventStr;
	self.title = options.title;
	self.dataSource = options.ajaxUrl;
	self.callback = options.callback;
	self.tempId = options.template?options.template:'chs-alert-temp';
	self.tempListId = options.listtemplate?options.listtemplate:'chs-alert-list-temp';
	self.handleInit();
	self.templateHelper();
}
//显示弹窗，并且初始化列表分页数据
chs_alert.prototype.alertRender = function(){
	var self = this;
	self.coditions.page = 1;
	var data = {
		title : self.title
	}
	var html = template(self.tempId, data);
	$('body').append(html);
	self.alertEveBind();
	self.listPager();
	self.searchInit();
	self.listRander();
}
//绑定搜索事件
chs_alert.prototype.searchInit = function(){
	var self = this;
	$(".chs-alert-search-btn").click(function(){
		self.coditions.name = $(this).siblings('input[name=matrial_name]').val();
		self.coditions.code = $(this).siblings('input[name=matrial_code]').val();
		self.coditions.model = $(this).siblings('input[name=matrial_type]').val();
		if(!self.coditions.name) delete self.coditions.name;
		if(!self.coditions.code) delete self.coditions.code;
		if(!self.coditions.model) delete self.coditions.model;
		
		self.listRander();
	})
}
//初始化触发弹窗的文档元素对象
chs_alert.prototype.handleInit = function(){
	var self = this;
	$(self.handle).on(self.eventStr, function(){
		self.alertRender();
	});
}
//为template添加辅助函数
chs_alert.prototype.templateHelper = function(){
	template.helper('jsonToStr', function(data){
		return JSON.stringify(data);
	})
}
//弹窗上按钮绑定对应的事件
chs_alert.prototype.alertEveBind = function(){
	var self = this;
	$(".chs-alert-close").click(function(){
		self.alertClose();
	})
	$(".chs-alert-cancel").click(function(){
		self.alertClose();
	})
}
//关闭弹窗的方法
chs_alert.prototype.alertClose = function(){
	$('.chs-alert-cover').remove();
}
//将ajax请求到的数据展示到弹窗列表里面
chs_alert.prototype.listRander = function(){
	var self = this;
	self.getData(self.dataSource, 'get', self.coditions, function(data){
		if(data[0]){
			data = JSON.parse(data);
			console.log(typeof data);
			var html = template(self.tempListId, {listData: data});
			$(".chs-alert-cont").html(html);
			self.pageData = data;
			self.listItemChs();
		}else{
			alert('没有更多内容了');
			if(self.coditions.page > 0) self.coditions.page --;
		}
	}, function(err){
		alert('网络错误');
	})
}
//为弹窗列表绑定点击事件，点击后传递对应数据给回调函数
chs_alert.prototype.listItemChs = function(){
	var self = this;
	$(".chs-alert-list-handle").click(function(){
		var dataKey = parseInt($(this).attr('data'));
		var chsData = self.pageData[dataKey];
		self.callback(chsData);
		self.alertClose();
	})
}
//弹窗底部的翻页按钮的事件绑定
chs_alert.prototype.listPager = function(){
	var self = this;
	$(".chs-alert-page-prev").click(function(){
		if(self.coditions.page <= 0){
			alert('已经是首页了');
		}else{
			self.coditions.page --;
			self.listRander();
		}
	})
	$(".chs-alert-page-next").click(function(){
		self.coditions.page ++;
		self.listRander();
	})
}
//隐藏或者显示等待动画
chs_alert.prototype.waiting_ctrl = function(action){
	var waiting = $(".chs-alert-waiting")
	if(action){
		waiting.show();
	}else{
		waiting.hide();
	}
}
//ajax请求方法，参数依次为ajax地址、请求方式（get，post）、提交的数据、成功后回调函数、失败的回调函数
chs_alert.prototype.getData = function(ajaxUrl, type, subData, successFn, errorFn){
	var self = this;
	self.waiting_ctrl(true);
	$.ajax({
		type:type,
		url:ajaxUrl,
		async:true,
		data: subData,
		timeout: 30000,
		success: function(data){
			if(data.code == 0){
				self.waiting_ctrl(false);
				if(successFn) successFn(data.data);
			}else{
				alert(data.message);
			}
		},
		error: function(err){
			self.waiting_ctrl(false);
			if(errorFn) errorFn(err);
		}
	});
}
